package com.dhcc.DRGsHosp.facade.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DictionaryDataApplication;
import com.dhcc.DRGsHosp.application.SystemConfigApplication;
import com.dhcc.DRGsHosp.application.impl.DrgsIndexCalApplicationImpl;
import com.dhcc.DRGsHosp.facade.InitializeFacade;
import com.dhcc.DRGsHosp.infra.util.GlobalData;

/**
 * 
 * @desp     ：    系统全局变量初始化类，系统启动和相关配置变化时会改变
 * @Author   ：lhj  
 * @ClassName: InitializeFacadeImpl  
 * @Date     : 2018年2月11日 上午11:17:00  
 * @Version  ：3.0.0 DHC.DRGs
 */
//@Named("drgsHospFacadeImpl25")
public class InitializeFacadeImpl implements InitializeFacade {
	@Autowired 
	private SystemConfigApplication systemConfigApplication;
	
	
	@Autowired
	private DictionaryDataApplication dictionaryDataApplication;
	
	
	private static final Logger LOGGER = LoggerFactory.getLogger(DrgsIndexCalApplicationImpl.class);
/**
 * 
 *  @Description：获取系统变量（修改）
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.InitializeFacade#initialize()
 */
	@Override
	public void initialize() {
		GlobalData.CONFIG_STD_YEAR = 1;
		GlobalData.CONFIG_MANAGE_YEAR = 1;
		GlobalData.CONFIG_MONITOR_YEAR = 12;	
		//初始化标杆总表的两条数据
		initializeManageSummary();
		/*系统配置全局初始化*/
		initializeSysConfig();
		/*系统字典配置字典*/
		initializeSysDictionary();
		//标杆比反推指标映射信息
		initializeStdQuotaInfo();
		//病例明细配置信息
		initializeCaseLinkCondition();
		//获取医院授权密码和名称
		initializeHospinfo();
	}
	/**
	 *  @Discription：获取医院基本信息
	 *  @Author： liuhongjun
	 *  @Date:2018年12月26日下午3:23:57
	 */
	private void initializeHospinfo() {
		Properties pro = new Properties();
		try {
			pro.load(new InputStreamReader(InitializeFacadeImpl.class.getClassLoader().getResourceAsStream("/license.properties"), "UTF-8"));
			String licesneCode=pro.getProperty("licenseCode");
			String hospName=pro.getProperty("hospName");
			GlobalData.hospName=hospName;
			GlobalData.licenseCode=licesneCode;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 *  @desp  ：系统配置变量
	 *  @Author： lhj
	 *  @Date  :2018年2月11日上午10:49:25
	 */
	@Override
	public void initializeSysConfig() {
		List<Map<String,Object>> mapList = systemConfigApplication.systemConfig() ;
		Map<String,String> mapConfig = new HashMap<String,String>() ;
		for(Map<String,Object> map : mapList) {
			String key  = map.get("CFG_CODE") == null ? null : map.get("CFG_CODE").toString() ;
			String value  = map.get("CFG_VALUE") == null ? null : map.get("CFG_VALUE").toString() ;
			mapConfig.put(key, value) ;
		}
		GlobalData.sysConfig=mapConfig;
	}
	/**
	 *  @Description：系统字典初始化
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.facade.InitializeFacade#initializeSysDictionary()
	 */
	@Override
	public void initializeSysDictionary() {
		Map<String,List<Map<String, Object>>> resultMap = new HashMap<>();
		List<String> list=dictionaryDataApplication.findDictionaryCode();
		if(!list.isEmpty()) {
			for(int i = 0;i<list.size();i++){
				List<Map<String, Object>> dicValueList = dictionaryDataApplication.findDictionaryDataByCode(list.get(i));
				resultMap.put(list.get(i),dicValueList);
			}	
		}
		GlobalData.sysDictionary=resultMap;
	}
	/**
	 *  initializeManageSummary.  
	 *  @Description：初始化标杆管理总表
	 *  @Author LiuHongjun 
	 *  @Date:2017年11月1日下午7:22:05
	 */
	private void initializeManageSummary() {
		systemConfigApplication.initializeManageSummary() ;

	}
	
	/**
	 * 
	 *  @Description：标杆比反推指标映射信息
	 *  @Author WENYI  
	 *  @see com.dhcc.DRGsHosp.facade.InitializeFacade#initializeStdQuotaInfo()
	 */
	@Override
	public void initializeStdQuotaInfo() {
		Map<String,String> result=new HashMap<>();
		InputStream ips = InitializeFacadeImpl.class.getResourceAsStream("/stdCalculationType.properties");
		try {
			Properties properties = new Properties();
			properties.load(ips);
			Set<Object> keys = properties.keySet();//返回属性key的集合  
		    for (Object key : keys) {  
		    	result.put(key+"", properties.get(key)+"");
		    }
		    GlobalData.stdQuotaInfo=result;
		}catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }finally {
            try {
            	ips.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}
	/**
	 * 
	 *  @Description：病例明细配置信息
	 *  @Author WENYI  
	 *  @see com.dhcc.DRGsHosp.facade.InitializeFacade#initializeCaseLinkCondition()
	 */
	@Override
	public void initializeCaseLinkCondition() {
		Map<String,String> result=new HashMap<>();
		InputStream ips = InitializeFacadeImpl.class.getResourceAsStream("/caseLinkCondition.properties");
		try {
			Properties properties = new Properties();
			properties.load(ips);
			Set<Object> keys = properties.keySet();//返回属性key的集合  
		    for (Object key : keys) {  
		    	result.put(key+"", properties.get(key)+"");
		    }
		    GlobalData.caseLinkCondition=result;
		}catch (IOException e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
        }finally {
            try {
            	ips.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}

}
